package com.quantum.util
{
	import flash.utils.Dictionary;

	public class DictionaryUtils
	{


		/**
		 * Extracts the keys out of the given dictionary and returns that as an array.
		 */
		public static function getKeys(map:Dictionary):Array
		{
			var keys:Array=[];

			for (var key:Object in map)
			{
				keys.push(key);
			}
			return keys;
		}

		/**
		 * Extracts the values out of the given dictionary and returns that as an array.
		 */
		public static function getValues(map:Dictionary):Array
		{
			var values:Array=[];

			for (var key:Object in map)
			{
				values.push(map[key]);
			}
			return values;
		}


		/**
		 * Checks whether the given dictionary is empty.
		 *
		 * @param map
		 *    Dictionary to check. Note that the dictionary can be both: new style
		 *    flash.utils.Dictionary class or just a plain old Object instance used
		 *    as Dictionary.
		 *
		 * @return
		 *    True, if the dictionary is empty; false otherwise.
		 */
		public static function isDictionaryEmpty(map:Object):Boolean
		{
			for (var key:Object in map)
			{
				return false;
			}
			return true;
		}

		/**
		 * Returns the number of items in the given dictionary.
		 */
		public static function getDictionaryLength(map:Object):int
		{
			var len:int;
			for (var key:Object in map)
			{
				++len;
			}
			return len;
		}

		/**
		 * Converts the given object into a dictionary. Currently supports only the enumerable
		 * collection types (i.e., types that can be iterated over using 'for each'). It maps
		 * each item in the input collection to its key (as defined by the getKeyPropertyValue
		 * method).
		 *
		 * <p>Returns null for a null input arg.
		 *
		 * @see #getKeyPropertyValue()
		 */
		public static function asDictionary(src:Object):Dictionary
		{
			if (src == null)
			{
				return null;
			}
			var map:Dictionary=new Dictionary();
			for each (var item:Object in src)
			{
				var key:*=null; //getKeyPropertyValue(item);
				if (key == null)
				{
					continue;
				}
				map[key]=item;
			}
			return map;
		}
	}
}